import axios from 'axios'
const instance = axios.create({
  baseURL: '/api', // 接口基础路径
  timeout: 20000 // 超时时间（毫秒）
})
// "Access-Control-Allow-Origin", "*"
// 添加请求拦截器
instance.interceptors.request.use(
  function (config) {
    // if (process.env.NODE_ENV == "production") {
    //   config.url = process.env.BASE_ROOT + config.url
    // }
    // 在请求发送之前做一些处理
    config.headers.access_token = window.sessionStorage.getItem('access_token')
    config.headers.uid = window.sessionStorage.getItem('uid')
    config.headers.Contenttype = 'application/x-www-form-urlencoded'
    return config
  },
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error)
  }
)

// 添加响应拦截器
instance.interceptors.response.use(
  function (response) {
    // 对响应数据做一些处理
    return response.data
  },
  function (error) {
    // 对响应错误做些什么
    return Promise.reject(error)
  }
)

// 封装 GET 请求方法
export const get = (url, params) => {
  return instance.get(url, {
    params
  }).data
}

// 封装 POST 请求方法
export const post = (url, data) => {
  return instance.post(url, data)
}

export const tipsFun = (that, options, content) => {
  if (options == 'success') {
    that.$message({
      message: content,
      type: 'success'
    })
  } else if (options == 'error') {
    that.$message.error(content)
  }
}

export const testFun = (options, content) => {
  if (options == 1) {
    // 数字验证
    var reg = /^\d+$/
    return reg.test(content)
  } else if (options == 2) {
    // 数字验证
    var reg = /^((\d{18})|([0-9x]{18})|([0-9X]{18}))$/
    console.log(reg.test(content))
    return reg.test(content)
  } else if (options == 'error') {
    that.$message.error(content)
  }
}
export let formatterCellval = (row, column, cellValue, index) => {
  // if (cellValue == 0) {
  //   return cellValue;
  // }
  if (!cellValue) {
    return '一'
  }
  return cellValue
}

export let pickerOptions = () => {
  return {
    shortcuts: [
      {
        text: '最近一周',
        onClick (picker) {
          const end = new Date()
          const start = new Date()
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
          picker.$emit('pick', [start, end])
        }
      },
      {
        text: '最近一个月',
        onClick (picker) {
          const end = new Date()
          const start = new Date()
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
          picker.$emit('pick', [start, end])
        }
      },
      {
        text: '最近三个月',
        onClick (picker) {
          const end = new Date()
          const start = new Date()
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
          picker.$emit('pick', [start, end])
        }
      }
    ]
  }
}

export let tabClick = (tab, event) => {
  console.log(tab, event)
}
